package com.example.recordingpen_datacollection.Utils.fileUtil;


import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

public class FileUtil {

    /**
     * 上传excel
     *
     * @param file
     * @param path
     */
    public static String upload(MultipartFile file, String path,String name) {
        String successFileName=null;
        if (file.isEmpty()) {
            throw new RuntimeException("上传失败，请上传文件");
        }
        String filePath = generateOperateSystemPath(path);
        createDir(filePath);
        String fileName = System.currentTimeMillis()+"-"+name;
        //String fileName = "E:/wx/123.xlsx";
        try {
            /*File newFile = new File(fileName);
            file.transferTo(newFile);*/
            byte[] bytes = file.getBytes();
            File fileToSave = new File(filePath + File.separator + fileName);
            FileCopyUtils.copy(bytes, fileToSave);
            successFileName=fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return successFileName;
    }

    //下载excel
    public static void download(HttpServletResponse response, String path, String filename) {
        String filePath = generateOperateSystemPath(path);
        createDir(filePath);
        File file = new File(filePath + File.separator + filename);
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            fis = new FileInputStream(file);
            response.setContentType("application/force-download");
            // 设置下载后的文件名以及header
            response.addHeader("Content-disposition", "attachment;fileName=" + filename);
            // 创建输出对象
            os = response.getOutputStream();
            // 常规操作
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = fis.read(buf)) != -1) {
                os.write(buf, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("文件不存在");
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    /**
     * 获得对应符合操作系统的文件路径
     *
     * @param path
     * @return
     */
    public static String generateOperateSystemPath(String path) {
        // windows下
        String replacement = "\\";
        String target = "/";

        if (replacement.equals(File.separator)) {
            path = path.replace(target, replacement);
        }
        // linux下
        if (target.equals(File.separator)) {
            path = path.replace(replacement, target);
        }
        return path;
    }

    /**
     * 创建文件夹
     *
     * @param dirPath
     * @return
     */
    public static File createDir(String dirPath) {

        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        return dir;
    }

    public static String getUuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static byte[] readFileByBytes(String filePath) throws IOException {
        File file = new File(filePath);
        byte[] bytesArray = new byte[(int) file.length()];
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            fis.read(bytesArray);
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
        return bytesArray;
    }
}
